package com.tianlu.store.service.impl;

import com.tianlu.store.common.MyBaseServiceImpl;
import com.tianlu.store.entity.SysRegion;
import com.tianlu.store.mapper.SysRegionMapper;
import com.tianlu.store.service.SysRegionService;
import com.tianlu.store.utils.RedisTemplateUtils;
import com.tianlu.store.utils.TreeMenuUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author 梁东烂
 * @Desc (SysRegion)表服务实现类
 * @Date 2023-04-28 18:17:35
 */
@Service("sysRegionService")
public class SysRegionServiceImpl extends MyBaseServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {
    private static final String REGION_KEY = "region_tree_data";
    @Override
    public List<SysRegion> regionTree() {
        Object obj = RedisTemplateUtils.get(REGION_KEY);
        if (Objects.nonNull(obj)) {
            return (List<SysRegion>) obj;
        }
        List<SysRegion> list = dao.findList(new SysRegion());
        List<SysRegion> regionList = TreeMenuUtil.treeOut(list, SysRegion::getId, SysRegion::getPid, SysRegion::getChildren);
        RedisTemplateUtils.set(REGION_KEY, regionList);
        return regionList;
    }
}
